﻿module tweetc.Utilities

open System
open System.Reflection
open System.Text.RegularExpressions

let display list = list |> List.iter (printfn "%s")

let cprintf color format =
    Printf.kprintf
        (fun s ->
            let current = Console.ForegroundColor
            Console.ForegroundColor <- color
            Console.Write(s)
            Console.ForegroundColor <- current)
        format 

let removeHtmlTags text =
    let rx = new System.Text.RegularExpressions.Regex("<[^>]*>");
    rx.Replace(text, "")

let wrap margin text =
    let mutable head = 0
    let mutable tail = margin
    let mutable lines = []
    let text = Regex.Replace(text, @"\s", " ").Trim()
    while tail < text.Length do
        while text.[tail] <> ' ' && tail > head do tail <- tail - 1
        if tail = head then tail <- head + margin
        lines <- lines @ [text.Substring(head, tail - head)]
        head <- tail + 1
        tail <- head + margin
    if head < text.Length then lines <- lines @ [text.Substring(head)]
    lines

let relativeTime (date:DateTime) =
    let gap = (DateTime.Now - date).TotalSeconds
    match gap with
    | _ when gap <= 60.0     -> string (int gap) + "s"
    | _ when gap <= 3600.0   -> string (int (gap/60.0)) + "m"
    | _ when gap <= 86400.0  -> string (int (gap/3600.0)) + "h"
    | _ when gap <= 604800.0 -> string (int (gap/86400.0)) + "d"
    | _                      -> date.ToString("dd MMM yy")

let buildDate() = 
    let assembly = Assembly.GetExecutingAssembly()
    assembly.GetName().Version.ToString()

let showHelp() =
    let help = [
        "tc - commmand line twitter client - " + buildDate();
        "";
        "usage: tweetc [option] [message]";
        "";
        "examples:";
        "  tc Hi Mom!               :tweet hello to your Mom";
        "  tc                       :get new tweets, mentions, messages";
        "  tc -t                    :get new tweets";
        "  tc -t50                  :get last 50 tweets (limit 200)";
        "  tc -m                    :get new mentions";
        "  tc -m50                  :get last 50 mentions (limit 200)";
        "  tc -d                    :get new direct messages";
        "  tc -d50                  :get last 50 messages (limit 200)";
        "  tc -Green                :set text color to green";
        "  tc -lDarkGreen           :set secondary color to dark green";
        "  tc -reset                :erase all remembered settings";
        "  tc -s paris hilton       :search tweets for paris hilton (loser)";
        "  tc -h                    :help";
        "";
        "Quotes for messages are optional and needed only to preserve spacing. Example:";
        "  tc Hi Mom!";
        "";
        "is the same as";
        "  tc \"Hi Mom!\"";
        "";
        "If tweetc takes more than a few seconds to fetch updates, it is likely that";
        "\"Automatic Proxy Dection\" (APD) is enabled. If you don't need APD, turn it";
        "off (Control Panel) and tweetc will fetch updates noticably faster.";
        "";
        "Valid color names:";
        "  Black"; 
        "  DarkBlue"; 
        "  DarkGreen";
        "  DarkCyan"; 
        "  DarkRed";
        "  DarkMagenta";
        "  DarkYellow";
        "  Gray";
        "  DarkGray"; 
        "  Blue";
        "  Green";
        "  Cyan";
        "  Red";
        "  Magenta";
        "  Yellow";
        "  White";
        "";
        "http://blueonionsoftware.com for more information. Donations appreciated!";]
    display help
